// 个人字典一次性请求多条数据
import { getDicts } from '@/api/system/personal-dict/data'
// 系统字典
import { getSysDicts } from '@/api/common/getSysDicts' // 查询字典类行中的数据

// 引入存储cookie
import {
  getProId,
  setProId,
  getExplorationId,
  setExplorationId,
  getProStatus,
  setProStatus
} from '@/utils/auth'

const state = {
  baiduAk: 'R3rtowdmLpSq7bUWm4qz2AnnvPcLgkLs', // 耿玉霞测试key
  proId: getProId() || '', // 当前项目id
  explorationId: getExplorationId() || '', // 当前勘探点id
  proStatus: getProStatus() || '', // 当前项目状态
  // 人员类型
  personType: [],
  // 单位类型
  orgType: [],
  // 设备类型
  equipmenType: [],
  // 勘探点类型
  explorationType: [],
  // 人员证件类型
  personnelCertType: [],
  // 企业证件类型
  enterpriseCertType: [],
  status: [
    {
      'dictLabel': '正常',
      'dictValue': '0'
    },
    {
      'dictLabel': '停用',
      'dictValue': '02'
    }
  ],
  // explorationBusinessType = 0:勘探点；1:物探；2:井探；3:槽探  这是勘探点类型选择时的判断依据，为了查看才加此备注
  proGrade: [], // 项目等级
  ProStatusList: [], // 项目状态列表
  ExplorationStatusList: [], // 勘探点类型
  regional: [] // 企业类别
}

const mutations = {
  // 修改当前项目id
  SET_ProId: (state, proId) => {
    state.proId = proId
    setProId(proId)
  },
  // 修改当前勘探点id
  SET_ExplorationId: (state, explorationId) => {
    state.explorationId = explorationId
    setExplorationId(explorationId)
  },
  // 修改当前项目状态
  SET_ProStatus: (state, proStatus) => {
    state.proStatus = proStatus
    setProStatus(proStatus)
  },
  // 人员类型
  SET_PersonType: (state, personType) => {
    state.personType = personType
  },
  // 单位类型
  SET_OrgType: (state, orgType) => {
    state.orgType = orgType
  },
  // 设备类型
  SET_EquipmenType: (state, equipmenType) => {
    state.equipmenType = equipmenType
  },
  // 勘探点类型全部
  SET_ExplorationType: (state, explorationType) => {
    state.explorationType = []
    state.explorationType = [{ dictValue: '', dictLabel: '全部' }, ...explorationType]
  },
  // 人员证件类型
  SET_PersonnelCertType: (state, personnelCertType) => {
    state.personnelCertType = personnelCertType
  },
  // 企业证件类型
  SET_EnterpriseCertType: (state, enterpriseCertType) => {
    state.enterpriseCertType = enterpriseCertType
  },
  // 20210526 新增
  // 项目等级
  SET_ProGrade: (state, proGrade) => {
    state.proGrade = proGrade
  },
  // 项目状态
  SET_ProStatusList: (state, ProStatusList) => {
    state.ProStatusList = []
    state.ProStatusList = [{ dictValue: '', dictLabel: '全部' }, ...ProStatusList]
  },
  // 勘探点状态
  SET_ExplorationStatusList: (state, ExplorationStatusList) => {
    state.ExplorationStatusList = []
    state.ExplorationStatusList = [{ dictValue: '', dictLabel: '全部' }, ...ExplorationStatusList]
  },
  // 企业类别
  SET_Regional: (state, regional) => {
    state.regional = regional
  }
}

const actions = {
  // 个人字典请求多条数据
  getDicts({ commit, state }) {
    const data = 'personnel_cert_type,enterprise_cert_type,exploration_type,equipmen_type,experimen_type,experimenTypeList,lithology_type,sample_type,soilSample_type,inSitu_type,prospecting_type'
    getDicts(data).then(res => {
      if (res) {
        // 企业证件类型
        commit('SET_EnterpriseCertType', res.data.enterprise_cert_type)
        // 人员证件类型
        commit('SET_PersonnelCertType', res.data.personnel_cert_type)
        // 设备类型
        commit('SET_EquipmenType', res.data.equipmen_type)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 系统字典
  // 人员类型
  getSysDictsPerson({ commit, state }) {
    getSysDicts('sys_basicInfo_personType').then(res => {
      if (res) {
        commit('SET_PersonType', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 单位类型
  getSysDictsOrgType({ commit, state }) {
    getSysDicts('sys_basicInfo_orgType').then(res => {
      if (res) {
        commit('SET_OrgType', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 项目等级
  getSysDictsProGrade({ commit, state }) {
    getSysDicts('sys_basicInfo_proGrade').then(res => {
      if (res) {
        commit('SET_ProGrade', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 项目状态
  getSysDictsProStatusList({ commit, state }) {
    getSysDicts('sys_project_status').then(res => {
      if (res) {
        commit('SET_ProStatusList', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 勘探点类型全部
  getSysDictsExplorationTypeList({ commit, state }) {
    getSysDicts('sys_all_exploration_type').then(res => {
      if (res) {
        commit('SET_ExplorationType', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 勘探点状态
  getSysDictsExplorationStatusList({ commit, state }) {
    getSysDicts('sys_exploration_status').then(res => {
      if (res) {
        commit('SET_ExplorationStatusList', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  // 勘探点状态
  getSysDictsRegional({ commit, state }) {
    getSysDicts('sys_regional').then(res => {
      if (res) {
        commit('SET_Regional', res.data.rows)
      } else {
        this.msgError(res.message)
      }
    })
  },
  getSysDicts({ dispatch }) {
    dispatch('getSysDictsProGrade')
    dispatch('getSysDictsProStatusList')
    dispatch('getSysDictsRegional')
    dispatch('getSysDictsExplorationTypeList')
    dispatch('getSysDictsExplorationStatusList')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
